package com.vini.web.pages;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.RequiredTextField;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.apache.wicket.util.cookies.CookieUtils;
import org.apache.wicket.validation.validator.StringValidator;

import com.vini.core.ec.User;
import com.vini.core.si.SignInService;
import com.vini.web.AppSession;

public class SignIn extends BasePage {

	private static final long serialVersionUID = 1L;
	protected static final String REMEMBER_ME_LOGIN_COOKIE = "app_login_name";
	protected static final String REMEMBER_ME_PASSWORD_COOKIE = "app_login_pwd";
	protected static final int REMEMBER_ME_DURATION_IN_DAYS = -1;
	private RequiredTextField<String> name;
	private PasswordTextField password;
	private CheckBox remember;
	private AjaxButton signIn;
	private AjaxButton forgetPassword;
	private Form<Void> signInForm;
	private Model<String> nameModel;
	private Model<String> pwdModel;
	@SpringBean
	private SignInService signInService;

	public SignIn() {
		checkCookies();
		
		signInForm = new Form<Void>("form");
		name = new RequiredTextField<String>("name", nameModel);
		password = new PasswordTextField("password", pwdModel);
		password.add(StringValidator.lengthBetween(6, 15));
		remember = new CheckBox("remember", new Model<Boolean>(false));
		signIn = new AjaxButton("signIn") {

			private static final long serialVersionUID = 1L;
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				User user = signInService.signIn(name.getModelObject(), password.getModelObject());
				if (user != null) {
					((AppSession)getSession()).setUser(user);
					System.out.println("Sign In Successfull..!!");
					if(remember.getModelObject()) {
						CookieUtils utils = new CookieUtils();
						utils.save(REMEMBER_ME_LOGIN_COOKIE, name.getModelObject());
						utils.save(REMEMBER_ME_PASSWORD_COOKIE, password.getModelObject());
					}else{
						CookieUtils utils = new CookieUtils();
						utils.remove(REMEMBER_ME_LOGIN_COOKIE);
						utils.remove(REMEMBER_ME_PASSWORD_COOKIE);
					}

					setResponsePage(Home.class);
				}else{
					error("Sign In Failed.. Try Again..");
					modalWindow.show(target);
				}
			}
			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				modalWindow.show(target);
			}

		};
		forgetPassword = new AjaxButton("forgetPassword") {

			private static final long serialVersionUID = 1L;
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// TODO Auto-generated method stub
				super.onSubmit(target, form);
			}

		};

		signInForm.add(name);
		signInForm.add(password);
		signInForm.add(remember);
		signInForm.add(signIn);
		signInForm.add(forgetPassword);
		this.add(signInForm);
	}

	private void checkCookies() {
		CookieUtils utils = new CookieUtils();
		nameModel = new Model<String>(utils.load(REMEMBER_ME_LOGIN_COOKIE));
		pwdModel = new Model<String>(utils.load(REMEMBER_ME_PASSWORD_COOKIE));
		User user = signInService.signIn(nameModel.getObject(), pwdModel.getObject());
		if (user != null) {
			((AppSession)getSession()).setUser(user);
			setResponsePage(Home.class);
		}
	}



}
